package edu.hit.crawler.util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class DomainExacter {

	public static String[] domainRules = { ".edu.cn", ".xxx", ".ws", ".vg",
			".us", ".tv", ".tk", ".tc", ".se", ".org.uk", ".org.tw", ".org.nz",
			".org.in", ".org.es", ".org.cn", ".org.ag", ".org", ".nu",
			".nom.es", ".nom.co", ".nl", ".net.nz", ".net.in", ".net.co",
			".net.cn", ".net.bz", ".net.br", ".net.ag", ".net", ".mx", ".ms",
			".mobi", ".me.uk", ".me", ".jobs", ".it", ".info", ".ind.in",
			".in", ".idv.tw", ".hk", ".gs", ".gen.in", ".fr", ".fm",
			".firm.in", ".eu", ".es", ".de", ".com.tw", ".tw", ".com.mx",
			".com.es", ".com.co", ".com.cn", ".com.bz", ".com.br", ".com.ag",
			".com", ".co.uk", ".co.nz", ".co.jp", ".jp", ".co.in", ".co",
			".cc", ".ca", ".bz", ".biz", ".be", ".at", ".asia", ".am", ".ag", ".ru", "cn" };

	public static String exact(String url) {

		try {
			url = new URL(url).getHost();
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		String tld = null;
		for (int i = 0; i < domainRules.length; ++i) {
			if (url.endsWith(domainRules[i])) {
				tld = domainRules[i];
				break;
			}
		}
		if (null == tld)
			return url;
		//System.out.println("tld : " + tld);
		url = url.replace(tld, "");
		url = url.substring(url.lastIndexOf(".")  +1);

		return url;
	}

	/**
	 * @param args
	 * @throws IOException
	 */
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new FileReader("seeds.txt"));
		// BufferedWriter bw = new BufferedWriter(new
		// FileWriter("testdomain.txt"));
		/*
		 * List<String> l = Arrays.asList(domainRules); Collections.sort(l);
		 * Collections.reverse(l); for (String s : l) { System.out.print(",\"" +
		 * s + "\""); }
		 */
		String line;

		while ((line = br.readLine()) != null) {
			System.out.println(line);
			System.out.println(exact(line));
		}
	}

}
